home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / tests / stdlib / stdlib.c < prev   
Encoding:
C/C++ Source or Header  |  1988-06-17  |  9.5 KB  |  500 lines

  1. /* 
  2.  * stdlib.c --
  3.  *
  4.  *    This file contains a program that exercises the stdlib
  5.  *    library facilities.  Invoke it with no parameters;  it
  6.  *    will print messages on stderr for any problems it detects
  7.  *    with the string procedures.
  8.  *
  9.  * Copyright 1988 Regents of the University of California
  10.  * Permission to use, copy, modify, and distribute this
  11.  * software and its documentation for any purpose and without
  12.  * fee is hereby granted, provided that the above copyright
  13.  * notice appear in all copies.  The University of California
  14.  * makes no representations about the suitability of this
  15.  * software for any purpose.  It is provided "as is" without
  16.  * express or implied warranty.
  17.  */
  18.  
  19. #ifndef lint
  20. static char rcsid[] = "$Header: stdlib.c,v 1.2 88/06/08 13:28:53 ouster Exp $ SPRITE (Berkeley)";
  21. #endif not lint
  22.  
  23. #include <sprite.h>
  24. #include <proc.h>
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include <sys/wait.h>
  29. #include <signal.h>
  30.  
  31. #define error(string) \
  32.     fprintf(stderr, string); \
  33.     exit(1);
  34.  
  35. int
  36. sortFunc(a, b)
  37.     char **a, **b;
  38. {
  39.     return strcmp(*a, *b);
  40. }
  41.  
  42. int
  43. checkDifferent(ptr, count)
  44.     int *ptr;            /* Pointer to array of integers to check
  45.                  * to see if they're all different. */
  46.     int count;            /* How many integers in array. */
  47. {
  48.     int i, j;
  49.  
  50.     for (i = 0; i < count-1; i++) {
  51.     for (j = i+1; j < count; j++) {
  52.         if (ptr[i] == ptr[j]) {
  53.         return 1;
  54.         }
  55.     }
  56.     }
  57.     return 0;
  58. }
  59.  
  60. int exitValue = 22;
  61.  
  62. void
  63. exitFunc1()
  64. {
  65.     _exit(exitValue);
  66. }
  67.  
  68. void
  69. exitFunc2()
  70. {
  71.     exitValue = 14;
  72. }
  73.  
  74. main()
  75. {
  76.     int result = 0, i;
  77.     long int l, m, n;
  78.     char *term, *arg, *p;
  79.     double d;
  80.     div_t dt;
  81.     ldiv_t ldt;
  82.     int nums[20], nums2[20];
  83.     static char *strings[] = {
  84.     "beta",
  85.     "alpha",
  86.     "gamma",
  87.     "delta",
  88.     "charlie",
  89.     "smoke",
  90.     "aleph"
  91.     };
  92.     union wait status;
  93.     int pid, savedStdout, pipes[2];
  94.     char line[100];
  95.     static struct sigvec vec;
  96.  
  97.     /*
  98.      * atof
  99.      */
  100.  
  101.     if (atof("1a") != 1.0) {
  102.     error("atof error 1\n");
  103.     }
  104.     if (atof(".123,") != .123) {
  105.     error("atof error 2\n");
  106.     }
  107.     if (atof("-47.e2") != -4700.0) {
  108.     error("atof error 3\n");
  109.     }
  110.     if (atof("+0.2E-2") != .002) {
  111.     error("atof error 4\n");
  112.     }
  113.     if (atof("-123456789.01234") != -123456789.01234) {
  114.     error("atof error 5\n");
  115.     }
  116.     if (atof("a") != 0.0) {
  117.     error("atof error 6\n");
  118.     }
  119.     d = atof("e");
  120.     if (d != 0.0) {
  121.     error("atof error 6\n");
  122.     }
  123.     if (atof("-e5") != 0.0) {
  124.     error("atof error 7\n");
  125.     }
  126.  
  127.     /*
  128.      * atoi
  129.      */
  130.  
  131.     if (atoi("") != 0) {
  132.     error("atoi error 1\n");
  133.     }
  134.     if (atoi("47") != 47) {
  135.     error("atoi error 2\n");
  136.     }
  137.     if (atoi("-42a") != -42) {
  138.     error("atoi error 3\n");
  139.     }
  140.     if (atoi("a") != 0) {
  141.     error("atoi error 4\n");
  142.     }
  143.     if (atoi("09/6") != 9) {
  144.     error("atoi error 5\n");
  145.     }
  146.     if (atoi("55:") != 55) {
  147.     error("atoi error 6\n");
  148.     }
  149.     if (atoi("+189223") != 189223) {
  150.     error("atoi error 7\n");
  151.     }
  152.  
  153.     /*
  154.      * atol
  155.      */
  156.  
  157.     if (atol("") != 0) {
  158.     error("atol error 1\n");
  159.     }
  160.     if (atol("47") != 47) {
  161.     error("atol error 2\n");
  162.     }
  163.     if (atol("-42a") != -42) {
  164.     error("atol error 3\n");
  165.     }
  166.     if (atol("a") != 0) {
  167.     error("atol error 4\n");
  168.     }
  169.     if (atol("09/6") != 9) {
  170.     error("atol error 5\n");
  171.     }
  172.     if (atol("55:") != 55) {
  173.     error("atol error 6\n");
  174.     }
  175.     if (atol("+189223") != 189223) {
  176.     error("atol error 7\n");
  177.     }
  178.  
  179.     /*
  180.      * strtod
  181.      */
  182.  
  183.     arg = "1a";
  184.     if (strtod(arg, &term) != 1.0) {
  185.     error("strtod error 1\n");
  186.     }
  187.     if (term != (arg+1)) {
  188.     error("strtod error 2\n");
  189.     }
  190.     arg = ".123,";
  191.     if (strtod(arg, &term) != .123) {
  192.     error("strtod error 3\n");
  193.     }
  194.     if (term != (arg+4)) {
  195.     error("strtod error 4\n");
  196.     }
  197.     arg = "-47.e2";
  198.     if (strtod(arg, &term) != -4700.0) {
  199.     error("strtod error 5\n");
  200.     }
  201.     if (term != (arg+6)) {
  202.     error("strtod error 6\n");
  203.     }
  204.     if (strtod("+0.2E-2", (char *) NULL) != .002) {
  205.     error("strtod error 7\n");
  206.     }
  207.     if (strtod("-123456789.01234", (char *) NULL) != -123456789.01234) {
  208.     error("strtod error 8\n");
  209.     }
  210.     arg = "a";
  211.     if (strtod(arg, &term) != 0.0) {
  212.     error("strtod error 9\n");
  213.     }
  214.     if (term != arg) {
  215.     error("strtod error 10\n");
  216.     }
  217.     if (strtod("e", (char *) NULL) != 0.0) {
  218.     error("strtod error 11\n");
  219.     }
  220.     arg = "-e5";
  221.     if (strtod(arg, &term) != 0.0) {
  222.     error("strtod error 12\n");
  223.     }
  224.     if (term != arg) {
  225.     error("strtod error 13\n");
  226.     }
  227.  
  228.     /*
  229.      * strtol
  230.      */
  231.  
  232.     arg = "442x";
  233.     if (strtol(arg, &term, 10) != 442) {
  234.     error("strtol error 1\n");
  235.     }
  236.     if (term != (arg+3)) {
  237.     error("strtol error 2\n");
  238.     }
  239.     arg = "+01009";
  240.     if (strtol(arg, &term, 0) != 0100) {
  241.     error("strtol error 3\n");
  242.     }
  243.     if (term != (arg+5)) {
  244.     error("strtol error 4\n");
  245.     }
  246.     arg = "0x1abcDefgh";
  247.     if (strtol(arg, &term, 0) != 0x1abcdef) {
  248.     error("strtol error 5\n");
  249.     }
  250.     if (term != (arg+9)) {
  251.     error("strtol error 6\n");
  252.     }
  253.     arg = "18";
  254.     if (strtol(arg, &term, 0) != 18) {
  255.     error("strtol error 7\n");
  256.     }
  257.     if (term != (arg+2)) {
  258.     error("strtol error 8\n");
  259.     }
  260.     if (strtol("-0x10", (char *) NULL, 16) != -16) {
  261.     error("strtol error 9\n");
  262.     }
  263.     if (strtol("j0k", (char *) NULL, 20) != 380) {
  264.     error("strtol error 10\n");
  265.     }
  266.     arg = "0xq";
  267.     if (strtol(arg, &term, 0) != 0) {
  268.     error("strtol error 11\n");
  269.     }
  270.     if (term != arg) {
  271.     error("strtol error 12\n");
  272.     }
  273.  
  274.     /*
  275.      * strtoul
  276.      */
  277.  
  278.     arg = "12345";
  279.     if (strtoul(arg, &term, 0) != 12345) {
  280.     error("strtoul error 1\n");
  281.     }
  282.     if (term != &arg[5]) {
  283.     error("strtoul error 2\n");
  284.     }
  285.     arg = "-22";
  286.     if (strtoul(arg, &term, 0) != 0) {
  287.     error("strtoul error 3\n");
  288.     }
  289.     if (term != arg) {
  290.     error("strtoul error 4\n");
  291.     }
  292.     arg = "+14";
  293.     if (strtoul(arg, &term, 0) != 0) {
  294.     error("strtoul error 5\n");
  295.     }
  296.     if (term != arg) {
  297.     error("strtoul error 6\n");
  298.     }
  299.  
  300.     /*
  301.      * abs
  302.      */
  303.  
  304.     if (abs(-1) != 1) {
  305.     error("abs error 1\n");
  306.     }
  307.     if (abs(22) != 22) {
  308.     error("abs error 2\n");
  309.     }
  310.     if (abs(0) != 0) {
  311.     error ("abs error 3\n");
  312.     }
  313.  
  314.     /*
  315.      * labs
  316.      */
  317.  
  318.     l = labs((long int) -1);
  319.     if (l != 1) {
  320.     error("labs error 1\n");
  321.     }
  322.     l = labs((long int) 22);
  323.     if (l != 22) {
  324.     error("labs error 2\n");
  325.     }
  326.     l = labs((long int) 0);
  327.     if (l != 0) {
  328.     error ("labs error 3\n");
  329.     }
  330.  
  331.     /*
  332.      * div
  333.      */
  334.  
  335.     dt = div(22, 3);
  336.     if ((dt.quot != 7) || (dt.rem != 1)) {
  337.     error("div error 1\n");
  338.     }
  339.     dt = div(-22, 3);
  340.     if ((dt.quot != -7) || (dt.rem != -1)) {
  341.     error("div error 2\n");
  342.     }
  343.     dt = div(22, -3);
  344.     if ((dt.quot != -7) || (dt.rem != 1)) {
  345.     error("div error 3\n");
  346.     }
  347.     dt = div(-22, -3);
  348.     if ((dt.quot != 7) || (dt.rem != -1)) {
  349.     error("div error 4\n");
  350.     }
  351.  
  352.     /*
  353.      * ldiv
  354.      */
  355.  
  356.     ldt = ldiv((long int) 22, (long int) 3);
  357.     if ((ldt.quot != (long int) 7) || (ldt.rem != (long int) 1)) {
  358.     error("ldiv error 1\n");
  359.     }
  360.     ldt = ldiv((long int) -22, (long int) 3);
  361.     if ((ldt.quot != (long int) -7) || (ldt.rem != (long int) -1)) {
  362.     error("ldiv error 2\n");
  363.     }
  364.     ldt = ldiv((long int) 22, (long int) -3);
  365.     if ((ldt.quot != (long int) -7) || (ldt.rem != (long int) 1)) {
  366.     error("ldiv error 3\n");
  367.     }
  368.     ldt = ldiv((long int) -22, (long int) -3);
  369.     if ((ldt.quot != (long int) 7) || (ldt.rem != (long int) -1)) {
  370.     error("ldiv error 4\n");
  371.     }
  372.  
  373.     /*
  374.      * qsort
  375.      */
  376.  
  377.     qsort((char *) strings, 7, sizeof(char *), sortFunc);
  378.     for (i = 0; i < 6; i++) {
  379.     if (strcmp(strings[i+1], strings[i]) < 0) {
  380.         error("qsort error 1\n");
  381.     }
  382.     }
  383.  
  384.     /*
  385.      * rand
  386.      */
  387.  
  388.     for (i = 0;  i < 20; i++) {
  389.     nums[i] = rand();
  390.     }
  391.     if (checkDifferent(nums, 20)) {
  392.     error("rand error 1\n");
  393.     }
  394.     srand(1);
  395.     for (i = 0; i < 20; i++) {
  396.     if (rand() != nums[i]) {
  397.         error("rand error 2\n");
  398.     }
  399.     }
  400.     srand(12345);
  401.     for (i = 0; i < 20; i++) {
  402.     nums2[i] = rand();
  403.     }
  404.     if (checkDifferent(nums2, 20)) {
  405.     error("rand error 3\n");
  406.     }
  407.     for (i = 0; i < 20; i++) {
  408.     if (nums2[i] == nums[i]) {
  409.         error("rand error 4\n");
  410.     }
  411.     }
  412.  
  413.     /*
  414.      * atexit
  415.      */
  416.  
  417.     if (fork() == 0) {
  418.     atexit(exitFunc1);
  419.     atexit(exitFunc2);
  420.     exit();
  421.     }
  422.     wait(&status);
  423.     if (status.w_T.w_Retcode != 14) {
  424.     error("atexit error 1\n");
  425.     }    
  426.  
  427.     /*
  428.      * setenv/getenv
  429.      */
  430.  
  431.     setenv("TEST1", "foo");
  432.     setenv("TEST2", "bar");
  433.     setenv("TEST1", "foo2");
  434.     setenv("TEST3", "helpMe");
  435.     p = getenv("TEST1");
  436.     if ((p == 0) || (strcmp(p, "foo2") != 0)) {
  437.     error("getenv error 1\n");
  438.     }
  439.     p = getenv("TEST2");
  440.     if ((p == 0) || (strcmp(p, "bar") != 0)) {
  441.     error("getenv error 2\n");
  442.     }
  443.     p = getenv("TEST3");
  444.     if ((p == 0) || (strcmp(p, "helpMe") != 0)) {
  445.     error("getenv error 3\n");
  446.     }
  447.     if (getenv("IdontEXIST") != 0) {
  448.     error("getenv error 4\n");
  449.     }
  450.  
  451.     /*
  452.      * abort
  453.      */
  454.  
  455.     pid = fork();
  456.     if (pid == 0) {
  457.     abort();
  458.     }
  459.     while (1) {
  460.     int pid2;
  461.     pid2 = wait3(&status, WUNTRACED, 0);
  462.     if (pid2 == -1) {
  463.         error("abort error 1\n");
  464.     }
  465.     if (status.w_status == 0) {
  466.         continue;
  467.     }
  468.     if (pid2 == pid) {
  469.         break;
  470.     }
  471.     }
  472.     kill(pid, SIGKILL);
  473.     if ((status.w_S.w_Stopval != WSTOPPED)
  474.         || (status.w_S.w_Stopsig != SIGQUIT)) {
  475.     error("abort error 2\n");
  476.     }
  477.  
  478.     /*
  479.      * system
  480.      */
  481.  
  482.     pipe(pipes);
  483.     savedStdout = dup(1);
  484.     dup2(pipes[1], 1);
  485.     system("echo This is just a test");
  486.     dup2(savedStdout, 1);
  487.     i = read(pipes[0], line, 99);
  488.     if (i < 0) {
  489.     error("system error 1\n");
  490.     }
  491.     line[i] = 0;
  492.     if (strcmp(line, "This is just a test\n") != 0) {
  493.     error("system error 2\n");
  494.     }
  495.     close(pipes[0]);
  496.     close(pipes[1]);
  497.  
  498.     return result;
  499. }
  500.